<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>车辆图片管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
    <script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>


    <!-- 引入bootstrap-fileinput -->
    <link rel="stylesheet" type="text/css" href="/bootstrap-fileinput/css/fileinput.min.css" />
    <script type="text/javascript" src="/bootstrap-fileinput/js/fileinput.min.js"></script>
    <script type="text/javascript" src="/js/bootstrap-fileinput/js/locales/zh.js"></script>

</head>

<body class="hold-transition skin-red sidebar-mini"  >
<!-- .box-body -->

<div class="box-header with-border">
    <h3 class="box-title">车辆图片管理</h3>
</div>

<div class="box-body">

    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                    <button type="button" class="btn btn-default" title="删除" onclick="deleteBatch()"><i class="fa fa-trash-o"></i> 删除</button>
                    <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                </div>
            </div>
        </div>

        <!--工具栏/-->

        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">ID</th>
                <th class="sorting">图片</th>
                <th class="sorting">图片类型主键</th>
                <th class="sorting">汽车ID</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <tbody id="img">
            </tbody>
        </table>
        <!--数据列表/-->
    </div>
    <!-- 数据表格 /-->
</div>
<!-- /.box-body -->
<!-- 分页 -->


<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">图片编辑</h3>
            </div>
            <div class="modal-body">

                <table class="table table-bordered table-striped"  width="800px">
                    <td>图片</td>
                    <td>
                        <table>
                            <tr>
                                <td>
                                    <label for="aimg">上传图片</label>
                                    <input type="file"class="form-control" name="myFile" id="aimg">
                                    <input type="text" name="bimg" id="bimg">
                                </td>
                            </tr>
                        </table>
                    </td>
                    </tr>
                    <tr>
                        <td>图片类型</td>
                        <td>
                            <select class="form-control" id="im">
                                <option th:each="im:${list}" th:value="${im.id}">[[${im.name}]]</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>汽车ID</td>
                        <td><input  class="form-control" placeholder="汽车ID" id="carId">  </td>
                    </tr>
                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addData()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>
<!-- 修改窗口 -->
<div class="modal fade" id="editModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel2">类型编辑</h3>
            </div>
            <div class="modal-body">

                <table class="table table-bordered table-striped"  width="800px">
                    <tr>
                        <td>图片类型</td>
                        <td>
                            <input type="hidden" id="imgId">
                            <select class="form-control" id="im2">
                                <option th:each="im:${list}" th:value="${im.id}">[[${im.name}]]</option>
                            </select>
                    </tr>
                    <tr>
                        <td>图片</td>
                        <td>
                            <table>
                                <tr>
                                    <td>
                                        <label for="uimg">上传图片</label>
                                        <input type="file"class="form-control" name="myFile" id="uimg">
                                        <input type="text" name="dimg" id="dimg">
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td>汽车ID</td>
                        <td><input  class="form-control" placeholder="汽车ID" id="carId2">  </td>
                    </tr>
                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateData()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>

</body>
<script type="text/javascript">
    $(function () {
        getData();
        fileInput();
        updatefileInput();

    });

    function getData() {
        $.ajax({
            url: "/img/getImgInfo",
            type: "get",
            data: {},
            dataType: "json",
            async: false,
            success: function (result) {
                if(result.code == 200){
                    var list = result.data;
                    var info = '';
                    $(list).each(function (i,e) {
                        info +='<tr>';
                        info +='<td><input  type="checkbox" name="box" value="'+e.id+'"></td>';
                        info +='<td>'+e.id+'</td>';
                        info +='<td>';
                        info +='<img alt="" src="'+e.imgUrl+'" width="100px" height="50px">';
                        info +='</td>';
                        info +='<td>'+e.imgTypeId+'</td>';
                        info +='<td>'+e.carId+'</td>';
                        info +='<td class="text-center">';
                        info +='<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal2" onclick="toUpdate('+e.id+')">修改</button>';
                        info +='</td>';
                        info +='</tr>';
                    });
                    $("#img").html(info);
                    //window.location.reload();
                }

            },
            error: function () {
                alert("系统异常,请联系管理员");
            }
        })
    }

    function fileInput(){
        $('#aimg').fileinput({
            language: 'zh',//语言：中文
            uploadUrl: "/img/upload", //上传地址
            browseClass: 'btn btn-primary', //按钮样式
            maxFileCount: 1,//最多支持上传的个数
            enctype: 'multipart/form-data',//序列化
            maxFileSize: 1024, //单位为kb, 如果为0表示不限制文件大小
            allowedFileExtensions: ['jpg', 'gif', 'png'],//限制上传文件的类型
        });
        $("#aimg").on("fileuploaded", function(event, data, previewId, index){
            if(data.response){
                $("#bimg").val(data.response.data)
            }
        })
    }

    function updatefileInput(){
        $('#uimg').fileinput({
            language: 'zh',//语言：中文
            uploadUrl: "/img/upload", //上传地址
            browseClass: 'btn btn-primary', //按钮样式
            maxFileCount: 1,//最多支持上传的个数
            enctype: 'multipart/form-data',//序列化
            maxFileSize: 1024, //单位为kb, 如果为0表示不限制文件大小
            allowedFileExtensions: ['jpg', 'gif', 'png'],//限制上传文件的类型
            initialPreviewAsData: true, // 是否初始化预览图片
            initialPreviewFileType: 'image', //回显文件的类型(初始化预览图片类型)
            initialPreview: ""+$("#dimg").val()+"" //图片地址
        });
        $("#uimg").on("fileuploaded", function(event, data, previewId, index){
            if(data.response){
                $("#dimg").val(data.response.data)
            }
        })
    }


    function addData() {
        var imgUrl = $("#bimg").val();
        var imgTypeId = $("#im").val();
        var carId = $("#carId").val();
        $.ajax({
            url: "/img/saveImgInfo",
            type: "post",
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify({
                imgUrl:imgUrl,imgTypeId:imgTypeId,carId:carId
            }),
            dataType: "json",
            async: true,
            success: function (result) {
                if(result.code == 200){
                    window.location.reload();
                }

            },
            error: function () {
                alert("系统异常,请联系管理员");
            }
        })

    }

    //修改回显
    function toUpdate(id) {
        $.ajax({
            url: "/img/toUpdate/"+id,
            type: "post",
            data: {},
            dataType: "json",
            async: false,
            success: function (result) {
                if(result.code == 200){
                    var img = result.data;
                    $("#imgId").val(img.id);
                    $("#dimg").val(img.imgUrl);
                    $("#im2").val(img.imgTypeId);
                    $("#carId2").val(img.carId);

                }
            },
            error: function () {
                alert("系统异常,请联系管理员");
            }
        })
    }



    function updateData() {
        var id = $("#imgId").val();
        var imgTypeId = $("#im2").val();
        var imgUrl = $("#dimg").val();
        var carId = $("#carId2").val();
        $.ajax({
            url: "/img/updateImgInfo",
            type: "post",
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify({
                id:id,imgUrl:imgUrl,imgTypeId:imgTypeId,carId:carId
            }),
            dataType: "json",
            async: true,
            success: function (result) {
                if(result.code == 200){
                    window.location.reload();
                }
            },
            error: function () {
                alert("系统异常,请联系管理员");
            }
        })
    }

    function deleteBatch() {
        var ids = [];
        $("[name=box]:checked").each(function (i,e) {
            ids.push(e.value);
        });

        if(ids.length <= 0){
            alert("请选择需要删除的行");
            return;
        }

        $.ajax({
            url:"/img/deleteBatch/"+ids,
            type : "post",
            data:{},
            dataType : "json",
            async : false,//同步
            success : function(result) {
                if(result.code == 200){
                    window.location.reload();
                }
            },
            error : function() {
                alert("系统异常,请联系管理员");
            }
        })

    }
</script>
</html>